package cn.choujay.tlias.controller;

import cn.choujay.tlias.common.Result;
import cn.choujay.tlias.domain.Dept;
import cn.choujay.tlias.domain.Emp;
import cn.choujay.tlias.service.DeptService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.Arrays;
import java.util.List;


@Slf4j
@CrossOrigin(allowCredentials = "true", exposedHeaders = "token")
@RestController
@RequestMapping("/dept")
public class DeptController {
    @Resource
    private DeptService deptService;


    //分页查询
    @GetMapping("/depts")
    public Result getAllDept(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        System.out.println("currentPage = " + currentPage);
        System.out.println("pageSize = " + pageSize);
        PageHelper.startPage(currentPage, pageSize);
        List<Dept> allDept = deptService.getAllDept();
        PageInfo<Dept> pageInfo = new PageInfo<>(allDept);
        return Result.success(pageInfo);
    }

    @PostMapping("/add")
    public Result addDept(@RequestBody Emp emp) {
        System.out.println(emp);
        Boolean flag = deptService.addDept(emp);
        return Result.success(flag);
    }


    //根据ids 删除部门
    @DeleteMapping("/delete")
    public Result deleteDept(@RequestParam(value = "ids", required = false) Integer[] ids) throws Exception {
        System.out.println(Arrays.toString(ids));
        return deptService.deleteDept(ids) ? Result.success() : Result.error("删除失败");
    }

    //修改部门
    @PutMapping("/update")
    public Result updateDeptById(@RequestBody Emp emp) {
        System.out.println(emp);
        return deptService.updateDeptById(emp) ? Result.success() : Result.error("更新成功");
    }


}
